package com.example.p6_big.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.p6_big.MoneyBean;
import com.example.p6_big.R;

import java.util.ArrayList;

public class MoneyAdapter extends RecyclerView.Adapter<MoneyAdapter.ViewHolder> {

    private Context context;
    private ArrayList<MoneyBean.DataBean.ListBean> list;
    private int adapterPosition;

    public MoneyAdapter(Context context, ArrayList<MoneyBean.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_money, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        MoneyBean.DataBean.ListBean listBean = list.get(position);
        Glide.with(context).load(listBean.getPic()).into(holder.iv_logo);
        holder.tv_title.setText(listBean.getName());
        holder.tv_title_count.setText("销售：" + String.valueOf(listBean.getSellCount()) + "个");
        holder.tv_title_sub.setText(String.valueOf("库存：" + listBean.getStockCount()) + "个");
        holder.tv_price.setText(String.valueOf(listBean.getPrice()) + "元");

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapterPosition = holder.getAdapterPosition();
                notifyDataSetChanged();

            }
        });
        if (adapterPosition == position) {
            Glide.with(context).load(R.drawable.sina_on).into(holder.iv_is_select);
        } else {
            Glide.with(context).load(R.drawable.sina_un).into(holder.iv_is_select);
        }


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView iv_is_select;
        public ImageView iv_logo;
        public TextView tv_title;
        public TextView tv_title_sub;
        public TextView tv_title_count;
        public TextView tv_price;

        public ViewHolder(@NonNull View rootView) {
            super(rootView);
            this.iv_is_select = (ImageView) rootView.findViewById(R.id.iv_is_select);
            this.iv_logo = (ImageView) rootView.findViewById(R.id.iv_logo);
            this.tv_title = (TextView) rootView.findViewById(R.id.tv_title);
            this.tv_title_sub = (TextView) rootView.findViewById(R.id.tv_title_sub);
            this.tv_title_count = (TextView) rootView.findViewById(R.id.tv_title_count);
            this.tv_price = (TextView) rootView.findViewById(R.id.tv_price);
        }
    }

}
